Ext.define('MyDesktop.NewsInfo', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'Ext.data.ArrayStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'grid-winNewsInfo',

    init: function () {
        this.launcher = {
            text: 'News info',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        var desktop = this.app.getDesktop();
        var me = this;
        var win = desktop.getWindow('grid-winNewsInfo');
        if (!win) {

            var recordCategory;

            var btnCreate = new Ext.Button({
                text: AdviceLang.Create,
                disabled: true,
                iconCls: 'add',
                handler: function () {
                    var wind = me.createNewsInfo(win.items.get('grid-NewsInfo'), 'Add', recordCategory);
                    wind.show();
                    var editor = KindEditor.create(document.getElementsByName('NewsContent')[0], { height: '270px' });
                }
            });

            var btnEdit = new Ext.Button({
                text: AdviceLang.Edit,
                disabled: true,
                iconCls: 'option',
                handler: function () {
                    var gridNewsInfo = win.items.get('grid-NewsInfo');
                    var recordNewsInfo = gridNewsInfo.getSelectionModel().getSelection();
                    if (recordNewsInfo == null || recordNewsInfo == undefined || recordNewsInfo.length == 0) {
                        Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                        return;
                    }
                    var wind = me.createNewsInfo(gridNewsInfo, 'Edit', recordCategory);
                    wind.show();
                    var editor = KindEditor.create(document.getElementsByName('NewsContent')[0], { height: '270px' });
                }
            });

            var btnDelete = new Ext.Button({
                text: AdviceLang.Delete,
                disabled: true,
                iconCls: 'remove',
                handler: function () {
                    var gridNewsInfo = win.items.get('grid-NewsInfo');
                    var recordNewsInfo = gridNewsInfo.getSelectionModel().getSelection();
                    if (recordNewsInfo == null || recordNewsInfo == undefined || recordNewsInfo.length == 0) {
                        Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                        return;
                    }
                    Ext.Ajax.request({
                        url: '../HotelWebService.asmx/DeleteNewsInfo',
                        success: function (response, opts) {
                            Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Success);
                            gridNewsInfo.getStore().reload();
                        },
                        failure: function (response, opts) {
                            Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Failed);
                        },
                        params: { NewsCode: recordNewsInfo[0].get('NewsCode') }
                    });
                }
            });

            var storeNewsCategory = new Ext.data.JsonStore({
                storeId: 'newsCategoryStore',
                proxy: {
                    type: 'ajax',
                    url: '../HotelWebService.asmx/ListNewsCategory',
                    reader: {
                        type: 'json',
                        root: 'newsCategorys',
                        idProperty: 'CategoryCode'
                    }
                },
                fields: ['CategoryCode', 'CategoryName', 'CategoryDesc', { name: 'CreateDate', type: 'date'}]
            });

            var storeNewsInfo = new Ext.data.JsonStore({
                storeId: 'NewsInfoStore',
                pageSize: 2,
                autoLoad: false,
                proxy: {
                    type: 'ajax',
                    url: '../HotelWebService.asmx/ListNewsInfo',
                    reader: {
                        type: 'json',
                        totalproperty: "total",
                        root: 'data',
                        idProperty: 'UserCode'
                    }
                },
                fields: ['NewsCode', 'CategoryCode', 'NewsName', 'NewsDesc', 'NewsContent', 'Creator', { name: 'CreateDate', type: 'date'}]
            });

            var girdNewsCategory = Ext.create('Ext.grid.Panel', {
                store: storeNewsCategory,
                region: 'west',
                collapsible: true,
                animCollapse: true,
                split: true,
                columns: [
                    {
                        text: AdviceLang.CategoryName,
                        width: 130,
                        sortable: true,
                        dataIndex: 'CategoryName'
                    }
                ],
                columnLines: true,
                width: 150,
                title: AdviceLang.NewsCategory,
                iconCls: 'icon-grid',
                margin: '5 5 5 5',
                cmargins: '5 0 5 5'
            });

            var girdNewsInfo = Ext.create('Ext.grid.Panel', {
                region: 'center',
                id: 'grid-NewsInfo',
                title: AdviceLang.NewsList,
                margin: '5 5 5 0',
                store: storeNewsInfo,
                columns: [
                    new Ext.grid.RowNumberer(),
                    {
                        text: AdviceLang.NewsCode,
                        flex: 1,
                        width: 100,
                        hidden: true,
                        sortable: true,
                        dataIndex: 'NewsCode'
                    },
                    {
                        hidden: true,
                        dataIndex: 'CategoryCode'
                    },
                    {
                        text: AdviceLang.NewsName,
                        width: 180,
                        sortable: true,
                        dataIndex: 'NewsName'
                    },
                    {
                        text: AdviceLang.NewsDesc,
                        width: 130,
                        sortable: true,
                        dataIndex: 'NewsDesc'
                    }, {
                        text: AdviceLang.Creator,
                        width: 100,
                        sortable: true,
                        dataIndex: 'Creator'
                    }, {
                        text: AdviceLang.CreateDate,
                        width: 100,
                        sortable: true,
                        dataIndex: 'CreateDate',
                        xtype: 'datecolumn',
                        format: 'Y-m-d'
                    }
                ],
                tbar: [btnCreate, '-', btnEdit, '-', btnDelete],
                bbar: Ext.create('Ext.PagingToolbar', {
                    store: storeNewsInfo,
                    displayInfo: true,
                    displayMsg: AdviceLang.PageDisplayMsg,
                    emptyMsg: AdviceLang.NoResult
                })
            });

            girdNewsCategory.on("itemdblclick", function () {
                recordCategory = girdNewsCategory.getSelectionModel().getLastSelected();
                if (recordCategory != null) {
                    btnCreate.enable();
                    btnDelete.enable();
                    btnEdit.enable();
                    girdNewsInfo.setTitle(AdviceLang.NewsList + '--(' + recordCategory.get('CategoryName') + ')');
                    storeNewsInfo.load({ params: { CategoryCode: recordCategory.get('CategoryCode'), startIndex: 0, limit: 2 }
                    });
                }
            })

            win = desktop.createWindow({
                id: 'grid-winNewsInfo',
                title: AdviceLang.NewsInfo,
                width: 740,
                height: 480,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'border',
                items: [girdNewsCategory, girdNewsInfo]
            });

            storeNewsCategory.load({
                callback: function (records, operation, success) {
                    if (success) {

                    }
                    else {

                    }
                },
                scope: this
            });
        }
        return win;
    },

    createNewsInfo: function (gridNewsInfo, type, recordCategory) {
        var desktop = this.app.getDesktop();
        var me = this;
        var winAdd = desktop.getWindow('win-addNewsInfo');
        var recordNewsInfo;
        if (type == 'Edit') {
            var recordNewsInfo = gridNewsInfo.getSelectionModel().getLastSelected();
            if (recordNewsInfo == null || recordNewsInfo == undefined) {
                Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                return;
            }
        }
        if (!winAdd) {
            var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
            //            var hd = {
            //                html: '<textarea id="editor_id" name="content" style="width:700px;height:300px;"></textarea>'
            //            };
            winAdd = Ext.create('Ext.window.Window', {
                id: 'win-addNewsInfo',
                title: recordNewsInfo == null ? AdviceLang.AddNewsInfo : AdviceLang.EditNewsInfo,
                width: 800,
                height: 400,
                iconCls: 'icon-grid',
                animCollapse: false,
                modal: true,
                closable: false,
                constrainHeader: true,
                layout: 'fit',
                items: [
                    {
                        xtype: 'form',
                        layout: 'form',
                        id: 'addNewsInfoForm',
                        url: recordNewsInfo == null ? '../HotelWebService.asmx/CreateNewsInfo' : '../HotelWebService.asmx/UpdateNewsInfo',
                        bodyPadding: '5 5 0',
                        width: 350,
                        border: false,
                        fieldDefaults: {
                            msgTarget: 'side',
                            labelWidth: 75
                        },
                        defaultType: 'textfield',
                        items: [{
                            hidden: true,
                            name: 'NewsCode',
                            submitValue: recordNewsInfo == null ? false : true,
                            value: recordNewsInfo == null ? '' : recordNewsInfo.get("NewsCode")
                        }, {
                            hidden: true,
                            name: 'CategoryCode',
                            value: recordCategory == null ? '' : recordCategory.get("CategoryCode")
                        }, {
                            fieldLabel: AdviceLang.NewsName,
                            afterLabelTextTpl: required,
                            name: 'NewsName',
                            anchor: '50%',
                            value: recordNewsInfo == null ? '' : recordNewsInfo.get("NewsName"),
                            allowBlank: false
                        }, {
                            fieldLabel: AdviceLang.NewsDesc,
                            name: 'NewsDesc',
                            anchor: '50%',
                            value: recordNewsInfo == null ? '' : recordNewsInfo.get("NewsDesc")
                        },
                        {
                            fieldLabel: AdviceLang.NewsContent,
                            name: 'NewsContent',
                            submitValue: false,
                            value: recordNewsInfo == null ? '' : recordNewsInfo.get("NewsContent")
                        },
                        {
                            hidden: true,
                            name: 'CreateDate',
                            submitValue: recordNewsInfo == null ? false : true,
                            value: recordNewsInfo == null ? '' : recordNewsInfo.get("CreateDate")
                        }],

                        buttons: [{
                            text: AdviceLang.Save,
                            handler: function () {
                                var form = this.up('form').getForm();
                                var index = KindEditor.instances.length;
                                var newsContent = KindEditor.instances[index - 1].html();
                                //var vauleObj = form.getValues();
                                if (form.isValid()) {
                                    form.submit({
                                        params: { NewsContent: newsContent },
                                        success: function (form, action) {
                                            //KindEditor.instances = null;
                                            winAdd.close();
                                            gridNewsInfo.getStore().reload();
                                        },
                                        failure: function (form, action) {
                                            Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Failed);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: AdviceLang.Cancel,
                            handler: function () {
                                winAdd.close();
                            }
                        }]
                    }
                ]
            })

            return winAdd;
        }
    }
});





